package cn.zch.soft.util.ftp.gitxlxl2010.quartz;
import cn.zch.soft.util.ftp.gitxlxl2010.bussiness.BussinessUtil;
import cn.zch.soft.util.ftp.gitxlxl2010.com.util.ConfigConst;
import cn.zch.soft.util.ftp.gitxlxl2010.com.util.DateFormatTime;
import cn.zch.soft.util.ftp.gitxlxl2010.ftp.ContinueFtp;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.util.Arrays;
import java.util.GregorianCalendar;
/**
 * 支持断点续传的FTP实用类
 *
 * @author xiongl
 * @version 0.3 实现中文目录创建及中文文件创建，添加对于中文的支持
 */
@Slf4j
public class FtpQuartz implements Job {
  private static BussinessUtil bussinessUtil;
  public static void main(String[] args) {
    try {
      ConfigConst.getConfigConst();
      ContinueFtp myFtp = new ContinueFtp();
      boolean b = myFtp.connect(ConfigConst.ftpserver, Integer.parseInt(ConfigConst.ftpport), ConfigConst.ftpuser, ConfigConst.ftppwd);
      log.info(b + "");
    }
    catch (Exception e) {
      // TO DO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private static boolean isRunning = false;
  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    // TO DO Auto-generated method stub
    synchronized (this) {
      if (isRunning) {
        log.info("当前job未完成");
        return;
      }
      isRunning = true;
    }
    ContinueFtp myFtp = new ContinueFtp();
    try {
      log.info("文件开始下载");
      JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
      bussinessUtil = (BussinessUtil) jobDataMap.get("myjob");
      myFtp.connect(ConfigConst.ftpserver, Integer.parseInt(ConfigConst.ftpport), ConfigConst.ftpuser, ConfigConst.ftppwd);
      log.info("ConfigConst.ftpserver = " + ConfigConst.ftpserver);
      log.info("ConfigConst.ftpport = " + ConfigConst.ftpport);
      log.info("ConfigConst.ftpuser = " + ConfigConst.ftpuser);
      log.info("ConfigConst.ftppwd = " + ConfigConst.ftppwd);
      log.info("ConfigConst.ftpgetdir = " + ConfigConst.ftpgetdir);
      log.info("ConfigConst.ftpfile = " + ConfigConst.ftpfile);
      String[] files = myFtp.getFileList(ConfigConst.ftpgetdir);
      log.info("files = " + Arrays.toString(files));
      String[] filenames = ConfigConst.ftpfile.split(",");
      log.info("filenames = " + Arrays.toString(filenames));
      this.downLoadAll(files, myFtp, filenames);
    }
    catch (Exception e) {
      log.info("连接FTP出错：" + e.getMessage());
    }
    finally {
      try {
        myFtp.disconnect();
      }
      catch (IOException e) {
        log.error(e.getMessage());
      }
      log.info("文件开始下载结束");
      synchronized (this) {
        isRunning = false;
      }
    }
  }
  private void downLoadAll(String[] allFiles, ContinueFtp myFtp, String[] files) throws IOException {
    if (allFiles == null) {
      log.info("远程文件列表为空");
      return;
    }
    for (String allFile : allFiles) {
      if (allFile.startsWith(files[0])) {
        String filepath = ConfigConst.ftptodir + DateFormatTime.datetoString(new GregorianCalendar(), 2) + "-" + allFile;
        String result = myFtp.download(ConfigConst.ftpgetdir + allFile, filepath).toString();
        log.info("下载资源" + allFile + "文件结果：" + result);
        if ("Download_New_Success".equals(result) || "Download_From_Break_Success".equals(result)) {
          if (bussinessUtil.getFlag()) {
            bussinessUtil.setFilepath1(filepath);
            bussinessUtil.setFilepath2("");
          }
          else {
            bussinessUtil.setFilepath1("");
            bussinessUtil.setFilepath2(filepath);
          }
          bussinessUtil.setFlag(!bussinessUtil.getFlag());
        }
      }
    }
  }
}
